myApp.onPageInit('search', function (page) {
    console.log("onPageInit " + page.name);

    var xhr,
        params = page.query,
        template = getDataTemplate(params.type),
        lastPage = false,
        searchContainer = $$(page.navbarInnerContainer).find('.searchbar'),
        ul = $$(page.container).find('.list-block ul'),
        empty = $$(page.container).find('.searchbar-not-found'),
        rangeContainer = $$(page.container).find('.searchbar-range'),
        ranges = rangeContainer.find('li');

    params.fileType = params.type;

    function selectRange(range) {
        params.dateType = range;
        ranges.each(function () {
            if ($$(this).data('range') == params.dateType) {
                $$(this).css('opacity', 1);
            } else {
                $$(this).css('opacity', 0.5);
            }
        });
    }
    ranges.click(function() {
        selectRange($$(this).data('range'));
    });
    selectRange('0');

    function loadPage() {
        if (xhr !== undefined) {
            xhr.abort();
        }
        xhr = $$.getJSONP(SERVER_PATH + 'search.do', params, function(json) {
            xhr = undefined;

            if (params.start === 0) {
                ul.children().remove();
            }
            if (json.code == 1) {
                lastpage = json.results.length < PAGE_SIZE;
                params.start += PAGE_SIZE;
                $$.each(json.results, function (index, value) {
                    preprocessData(params.type, value);
                    var html = template(value);
                    ul.append(html);
                });
            }

            if (params.title !== 0 && ul.children().length === 0) {
                empty.show();
            } else {
                empty.hide();
            }
        });
    }

    var mySearchbar = myApp.searchbar(searchContainer, {
        customSearch: true,
        onSearch: function(s) {
            console.log('onSearch ' + s.query);

            params.title = s.query;
            params.start = 0;
            params.num = PAGE_SIZE;

            if (params.title.length === 0) {
                ul.children().remove();
                empty.hide();
                rangeContainer.show();
            } else {
                rangeContainer.hide();
                loadPage();
            }
        },
        onClear: function(s) {
            console.log('onClear');
            ul.children().remove();
            empty.hide();
            rangeContainer.show();
        },
        onDisable: function () {
            console.log('onDisable');
            page.view.back();
        }
    });
    $$(document).once('pageAfterAnimation', '.page[data-page="search"]', function (e) {
        mySearchbar.enable();
    });
    $$(document).once('pageBeforeRemove', '.page[data-page="search"]', function (e) {
        mySearchbar.destroy();
    });

    $$(page.container).find('.infinite-scroll').on('infinite', function () {
        if (!lastpage || !xhr) {
            return;
        }

        loadPage();
    });
});